package com.sz.web.util;

import com.sz.web.account.entity.AccountUser;
import com.sz.web.common.enums.SessionKey;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户工具类
 */
public class WebUserHelper {

    /**
     * 获取当前session中的用户信息
     * @return
     */
    public static Object getAccountInfo(){
        HttpSession session = getSession();
        AccountUser accountUser = (AccountUser) session.getAttribute(SessionKey.ACCOUNTINFO.getStrKey());
        return accountUser;
    }

    /**
     * 设置用户信息
     * @param object
     */
    public static void setAccountInfo(Object object){
        HttpSession session = getSession();
        session.setAttribute(SessionKey.ACCOUNTINFO.getStrKey(),object);
    }
    public static HttpSession getSession(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(600);
        return session;
    }
}
